package org.example.checkStore.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import org.example.checkStore.domain.CheckWarehouseStoreDetail;
import org.example.checkStore.mapper.CheckWarehouseStoreDetailMapper;
import org.example.warehouse.domain.WarehouseStore;
import org.example.warehouse.mapper.WarehouseStoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.checkStore.mapper.CheckWarehouseStoreMapper;
import org.example.checkStore.domain.CheckWarehouseStore;
import org.example.checkStore.service.ICheckWarehouseStoreService;
import com.ruoyi.common.core.text.Convert;

/**
 * 盘点仓库Service业务层处理
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-04-26
 */
@Service
public class CheckWarehouseStoreServiceImpl implements ICheckWarehouseStoreService
{
    @Autowired
    private CheckWarehouseStoreMapper checkWarehouseStoreMapper;

    @Autowired
    private CheckWarehouseStoreDetailMapper checkWarehouseStoreDetailMapper;

    @Autowired
    private WarehouseStoreMapper warehouseStoreMapper;

    /**
     * 查询盘点仓库
     *
     * @param checkWarehouseStoreId 盘点仓库主键
     * @return 盘点仓库
     */
    @Override
    public CheckWarehouseStore selectCheckWarehouseStoreByCheckWarehouseStoreId(Long checkWarehouseStoreId)
    {
        return checkWarehouseStoreMapper.selectCheckWarehouseStoreByCheckWarehouseStoreId(checkWarehouseStoreId);
    }

    /**
     * 查询盘点仓库列表
     *
     * @param checkWarehouseStore 盘点仓库
     * @return 盘点仓库
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<CheckWarehouseStore> selectCheckWarehouseStoreList(CheckWarehouseStore checkWarehouseStore)
    {
        return checkWarehouseStoreMapper.selectCheckWarehouseStoreList(checkWarehouseStore);
    }

    /**
     * 新增盘点仓库
     *
     * @param checkWarehouseStore 盘点仓库
     * @return 结果
     */
    @Override
    public int insertCheckWarehouseStore(CheckWarehouseStore checkWarehouseStore)
    {
        checkWarehouseStore.setCreateBy(ShiroUtils.getUserId()+"");
        checkWarehouseStore.setCreateTime(DateUtils.getNowDate());
        checkWarehouseStore.setCheckWarehouseStoreNo(DateUtils.dateTimeNow());
        int i=checkWarehouseStoreMapper.insertCheckWarehouseStore(checkWarehouseStore);

        if(checkWarehouseStore.getParams().containsKey("list")){
          String str =  checkWarehouseStore.getParams().get("list").toString();
            List<CheckWarehouseStoreDetail> list = JSONObject.parseArray(str, CheckWarehouseStoreDetail.class);
            list.forEach(item->{
                item.setCreateBy(ShiroUtils.getUserId()+"");
                item.setCreateTime(DateUtils.getNowDate());
                item.setCheckWarehouseDetailNo(DateUtils.dateTimeNow());
                item.setCheckWarehouseStoreId(checkWarehouseStore.getCheckWarehouseStoreId());
                checkWarehouseStoreDetailMapper.insertCheckWarehouseStoreDetail(item);

                WarehouseStore warehouseStore = new WarehouseStore();
                warehouseStore.setWarehouseStoreId(item.getWarehouseStoreId());
                warehouseStore.setCheckNum(item.getCheckNum());
                warehouseStoreMapper.updateWarehouseStore(warehouseStore);
            });

        }
        return i;
    }

    /**
     * 修改盘点仓库
     *
     * @param checkWarehouseStore 盘点仓库
     * @return 结果
     */
    @Override
    public int updateCheckWarehouseStore(CheckWarehouseStore checkWarehouseStore)
    {
        return checkWarehouseStoreMapper.updateCheckWarehouseStore(checkWarehouseStore);
    }

    /**
     * 批量删除盘点仓库
     *
     * @param checkWarehouseStoreIds 需要删除的盘点仓库主键
     * @return 结果
     */
    @Override
    public int deleteCheckWarehouseStoreByCheckWarehouseStoreIds(String checkWarehouseStoreIds)
    {
        return checkWarehouseStoreMapper.deleteCheckWarehouseStoreByCheckWarehouseStoreIds(Convert.toStrArray(checkWarehouseStoreIds));
    }

    /**
     * 删除盘点仓库信息
     *
     * @param checkWarehouseStoreId 盘点仓库主键
     * @return 结果
     */
    @Override
    public int deleteCheckWarehouseStoreByCheckWarehouseStoreId(Long checkWarehouseStoreId)
    {
        return checkWarehouseStoreMapper.deleteCheckWarehouseStoreByCheckWarehouseStoreId(checkWarehouseStoreId);
    }
}
